package com.example.dday11.controller;

import com.example.dday11.entity.User;
import com.example.dday11.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author Linyh
 * @Description
 * @Date 2022/7/18 16:31
 **/
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/selectAll")
    public List<User> selcetAllUser(){
        List<User> userList = userService.selectAllUser();
        return userList;
    }

    @RequestMapping("/selectById")
    public User selectUserById(@RequestParam int id){
        User user = userService.selectUserId(id);
        return user;
    }

    @RequestMapping("/add")
    public int addUser(@RequestBody User user){
        int rows=userService.addUser(user);
        if (rows!=0){
            System.out.println("新增成功");
        }else {
            System.out.println("新增失败");
        }
        return rows;
    }

    @RequestMapping("/update")
    public int updateUser(@RequestBody User user){
        int rows=userService.updateUser(user);
        if (rows!=0){
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }
        return rows;
    }

    @RequestMapping("/delete")
    public int deleteUser(@RequestParam int id){
        int rows=userService.deleteUser(id);
        if (rows!=0){
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
        return rows;
    }

}
